package com.zhss.eshop.auth.service.Impl;


import com.zhss.eshop.auth.domain.model.AuthPriority;
import com.zhss.eshop.auth.mapper.AuthPriorityMapper;
import com.zhss.eshop.auth.service.Impl.operation.PriorityNode;
import com.zhss.eshop.auth.service.Impl.operation.PriorityOperation;
import com.zhss.eshop.common.constant.CollectionSize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询授权权限的操作
 * @author suyuan
 *
 */
@Component
@Scope("prototype")
public class QueryAuthorizedPriorityOperation implements PriorityOperation{

	/**
	 * 账号id
	 */
	private Long accountId;

	/**
	 * 权限管理DAO组件
	 */
	@Autowired
	private AuthPriorityMapper priorityDAO;

	/**
	 * 执行这个操作
	 */
	@Override
	public Boolean visit(PriorityNode priority) throws Exception {
		List<PriorityNode> targetChildren = new ArrayList<PriorityNode>();

		Map<String, Object> parameters = new HashMap<String, Object>(CollectionSize.DEFAULT);
		parameters.put("accountId", accountId);
		parameters.put("parentId", priority.getId());

		List<AuthPriority> children = priorityDAO.listAuthroziedByAccountId(parameters);
		for(AuthPriority child : children) {
			//先拿到每个子节点，在对每个节点进行递归的操作
			PriorityNode targetChild = child.clone(PriorityNode.class);
			targetChild.accept(this);
			targetChildren.add(targetChild);
		}

		priority.setChildren(targetChildren);

		return true;
	}

	public Long getAccountId() {
		return accountId;
	}

	public void setAccountId(Long accountId) {
		this.accountId = accountId;
	}

}
